﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;



namespace MediaPlayer
{
    
  public static class CrowMedia
    {
        public static byte[] GetScreenshot(this UIElement source, double scale, int quality)
        {
            double actualHeight = source.RenderSize.Height;
            double actualWidth = source.RenderSize.Width;
            double renderHeight = actualHeight * scale;
            double renderWidth = actualWidth * scale;

            RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Default);
            VisualBrush sourceBrush = new VisualBrush(source);

            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();
            using (drawingContext)
            {
                drawingContext.PushTransform(new ScaleTransform(scale, scale));
                drawingContext.DrawRectangle(sourceBrush, null, new Rect(new System.Windows.Point(0, 0), new System.Windows.Point(actualWidth, actualHeight)));
            }
            renderTarget.Render(drawingVisual);
            JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
            jpgEncoder.QualityLevel = quality;
            jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));

            Byte[] imageArray;
            using (MemoryStream outputStream = new MemoryStream())
            {
                jpgEncoder.Save(outputStream);
                imageArray = outputStream.ToArray();
            }


            return imageArray;

        }

        public static void SaveScreenshot(this UIElement mediaElement, string DirectoryName)
        {
            byte[] screenshot = mediaElement.GetScreenshot(1, 90);
            string filename = @"\screenshot[" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second.ToString() + "].jpg";
            if (Directory.Exists(DirectoryName))
            {
                filename = DirectoryName + filename;
                FileStream fileStream = new FileStream(filename, FileMode.Create);
                BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                binaryWriter.Write(screenshot);
                binaryWriter.Close();
            }
            else
            {
                string message = "Do you want to create folder";
                string caption = "Folder dosn't exist";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult result;
                result = System.Windows.Forms.MessageBox.Show(Handle, message, caption, buttons);

                if (result == DialogResult.Yes)
                {

                    Directory.CreateDirectory(DirectoryName);
                    
                }
            }
        }

             public static IWin32Window Handle { get; set; }

     public static bool IsPointInRect(this System.Windows.Shapes.Rectangle r, System.Windows.Point p)
    {
      if (((p.X > r.RenderedGeometry.Bounds.TopLeft.X + 50) && (p.X < r.RenderedGeometry.Bounds.TopRight.X))&&((p.Y >= r.RenderedGeometry.Bounds.TopLeft.Y + 50) && (p.Y <= r.RenderedGeometry.Bounds.BottomLeft.Y)))
            return true;
        else
            return false;

                            
    }

    }
}
